﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureWallParametersCustomizer.h"
#include "DbObjectUtils.h"
#include "GbmpBuiltInParameterDefinitionsNew.h"
#include "GcmpBuiltInParameterDefinitions.h"
#include "IInstance.h"
#include "IParameterDefinitionLibrary.h"
#include "IParameterValueElementId.h"
#include "StructureInstancePresetParameterUtils.h"
#include "StructureInstanceSecondKeys.h"
#include "IPresetParameterManager.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

DBOBJECT_DATA_DEFINE(StructureWallParametersCustomizer)
{
    SetOwnerElement(nullptr);
}

StructureWallParametersCustomizer::StructureWallParametersCustomizer(IElement* pElement) :
    GmStructureDefaultElementParametersCustomizer(pElement)
{
}

bool StructureWallParametersCustomizer::IsPresetParameter(const UniIdentity& paramDefUid) const
{
    if (paramDefUid.HasSameValue(PARAMETER_DEF(ElementAssociatedLevelBuiltInParameter)->GetUid()) ||
        paramDefUid.HasSameValue(PARAMETER_UID(BuildingStoreyBuiltInParameter)) ||
        paramDefUid.HasSameValue(PARAMETER_DEF(ConcreteStrengthGradeBuiltInParameter)->GetUid())
        )
    {
        return true;
    }

    const IInstance *pInstance = quick_cast<IInstance>(GetOwnerElement());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pInstance, L"OwnerElement为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = pInstance->GetDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IPresetParameterManager* pPreSetMgr = IPresetParameterManager::Get(pDoc);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pPreSetMgr, L"pPreSetMgr为空",L"GDMPLab",L"2024-03-30");

    Int32 secondKey = pPreSetMgr->GetPresetValuesGroupId(pInstance);

    // 变截面墙
    if (secondKey == (Int32)StructureWallType::VariableSection)
    {
        if (paramDefUid.HasSameValue(PARAMETER_DEF(WallStartHeightBuiltInParameter)->GetUid()) ||
            paramDefUid.HasSameValue(PARAMETER_UID(BuildingStoreyBuiltInParameter)) ||
            paramDefUid.HasSameValue(PARAMETER_DEF(WallEndHeightBuiltInParameter)->GetUid()) ||
            paramDefUid.HasSameValue(PARAMETER_DEF(StartBottomAssociatedLevelOffsetBuiltInParameter)->GetUid()) ||
            paramDefUid.HasSameValue(PARAMETER_DEF(EndBottomAssociatedLevelOffsetBuiltInParameter)->GetUid()) ||
            paramDefUid.HasSameValue(PARAMETER_DEF(StartHeightBuiltInParameter)->GetUid()) ||
            paramDefUid.HasSameValue(PARAMETER_DEF(EndHeightBuiltInParameter)->GetUid())
            )
        {
            return true;
        }
    }
    else // 基本墙
    {
        if (paramDefUid.HasSameValue(PARAMETER_DEF(WallHeightBuiltInParameter)->GetUid()) ||
            paramDefUid.HasSameValue(PARAMETER_DEF(TopAssociatedLevelBuiltInParameter)->GetUid()) ||
            paramDefUid.HasSameValue(PARAMETER_DEF(BottomOffsetBuiltInParameter)->GetUid()) ||
            paramDefUid.HasSameValue(PARAMETER_DEF(TopOffsetBuiltInParameter)->GetUid())
            )
        {
            return true;
        }
    }

    return false;
}

void StructureWallParametersCustomizer::FilterParameterValues(const UniIdentity& paramDefUid,
    std::vector<OwnerPtr<IParameterValueStorage>>& values) const
{
    // 墙的定位方式为底标高+顶标高或底标高+墙高，其默认顶标高为Invalid，默认下拉列表默认有“< 空 >”
    if (paramDefUid.HasSameValue(PARAMETER_UID(TopAssociatedLevelBuiltInParameter)))
    {
        values.push_back(IParameterValueElementId::Create(ElementId::InvalidID));
    }
}
